package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patrol.domain.PtSafetyHazardReporting;
import com.ruoyi.patrol.domain.vo.PtSafetyHazardReportingVO;
import com.ruoyi.patrol.service.IPtSafetyHazardReportingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安全隐患上报Controller
 *
 * @author zwq
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/patrol/reporting")
public class PtSafetyHazardReportingController extends BaseController {
    @Autowired
    private IPtSafetyHazardReportingService ptSafetyHazardReportingService;

    /**
     * 查询统计
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:list')")
    @GetMapping("/total")
    public AjaxResult Total() {
        return AjaxResult.success(ptSafetyHazardReportingService.selectPtSafetyHazardReportingVOTotal());
    }

    /**
     * 查询安全隐患上报列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtSafetyHazardReporting ptSafetyHazardReporting) {
        startPage();
        List<PtSafetyHazardReportingVO> list = ptSafetyHazardReportingService.selectPtSafetyHazardReportingList(ptSafetyHazardReporting);
        return getDataTable(list);
    }

    /**
     * 导出安全隐患上报列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:export')")
    @Log(title = "安全隐患上报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtSafetyHazardReporting ptSafetyHazardReporting) {
        List<PtSafetyHazardReportingVO> list = ptSafetyHazardReportingService.selectPtSafetyHazardReportingList(ptSafetyHazardReporting);

        ExcelUtil<PtSafetyHazardReportingVO> util = new ExcelUtil<PtSafetyHazardReportingVO>(PtSafetyHazardReportingVO.class);
        return util.exportExcel(list, "安全隐患上报数据");
    }

    /**
     * 获取安全隐患上报详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        PtSafetyHazardReporting report = new PtSafetyHazardReporting();
        report.setId(id);
        return AjaxResult.success(ptSafetyHazardReportingService.selectPtSafetyHazardReportingByIdFilter(report));
    }

    /**
     * 新增安全隐患上报
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:add')")
    @Log(title = "安全隐患上报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtSafetyHazardReporting ptSafetyHazardReporting) {
        if (ptSafetyHazardReporting.getSafetyHazardLevel() == null) {
            return AjaxResult.error("安全隐患等级不能为空！");
        }
//        if (StringUtils.isEmpty(ptSafetyHazardReporting.getSafetyHazardReason())) {
//            return AjaxResult.error("安全隐患原因不能为空！");
//        }
        return toAjax(ptSafetyHazardReportingService.insertPtSafetyHazardReporting(ptSafetyHazardReporting));
    }

    /**
     * 修改安全隐患上报
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:edit')")
    @Log(title = "安全隐患上报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtSafetyHazardReporting ptSafetyHazardReporting) {
        return toAjax(ptSafetyHazardReportingService.updatePtSafetyHazardReporting(ptSafetyHazardReporting));
    }

    /**
     * 删除安全隐患上报
     */
    @PreAuthorize("@ss.hasPermi('patrol:reporting:remove')")
    @Log(title = "安全隐患上报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ptSafetyHazardReportingService.deletePtSafetyHazardReportingByIds(ids));
    }
}
